<?php
App::uses('AppController', 'Controller');
/**
 * Photos Controller
 *
 * @property Photo $Photo
 */
class PhotosController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Photo->recursive = 0;
		$this->paginate = array('order' => array('post_time' => 'DESC'));
		$this->set('photos', $this->paginate('Photo'));
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Photo->id = $id;
		if (!$this->Photo->exists()) {
			throw new NotFoundException(__('Invalid photo'));
		}
		$this->set('photo', $this->Photo->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		
		if ($this->request->is('post')) {
			$this->Photo->create();
			$newId = $this->generatePassword(45);
			while ($this->Photo->find('first', array('conditions' => array('Photo.photo_id' => $newId))) != null) {
				$newId = $this->generatePassword(45);
			}
			$this->request->data['Photo']['photo_id'] = $newId;
			$this->request->data['Photo']['button_type_rating'] = "0";
			$this->request->data['Photo']['longitude'] = 1000;
			$this->request->data['Photo']['latitude'] = 1000;
			$this->request->data['Photo']['location_latitude'] = 1000;
			$this->request->data['Photo']['location_longitude'] = 1000;
			$this->request->data['Photo']['location_text'] = "";
			$this->request->data['Photo']['post_time'] = date('Y-m-d H:i:s', strtotime('-1 day'));
			$this->request->data['Photo']['number_click_button'] = 0;
			$this->request->data['Photo']['delete_flag'] = "0";
			$this->request->data['Photo']['post_model'] = "ANDROID";
			//$this->request->data['Photo']['caption'] = $newId;
			$this->request->data['Photo']['point'] = 0;
			$this->request->data['Photo']['filter_id'] = 0;
			$this->request->data['Photo']['link_facebook'] = "0";
			$this->request->data['Photo']['link_twitter'] = "0";
			$this->request->data['Photo']['link_weibo'] = "0";
			$this->request->data['Photo']['link_tumblr'] = "0";
			$this->request->data['Photo']['link_flickr'] = "0";
			$this->request->data['Photo']['location_id'] = "-1";
			$newUrlId = $this->generatePassword(6);
			while ($this->Photo->find('first', array('conditions' => array('Photo.url_id' => $newUrlId))) != null) {
				$newUrlId = $this->generatePassword(6);
			}
			$this->request->data['Photo']['url_id'] = $newUrlId;
			if ($this->Photo->save($this->request->data)) {
				$this->Session->setFlash(__('The photo has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The photo could not be saved. Please, try again.'));
			}
		}
		$photos = $this->Photo->find('list');
		$arrayUser = array('binhbt', 'duytx', 'vinhnn', 'khoada', 'namnt', 'tungnt', 'tucq', 'huelt', 'hoalt', 'tuanna');
		$users = $this->Photo->User->find('list', array('fields' => array('user_name'), 'conditions' => array('user_name' => $arrayUser)));
		$this->set(compact('photos', 'users'));
	}
	
function generatePassword ($length = 8)
  {

    // start with a blank password
    $password = "";

    // define possible characters - any character in this string can be
    // picked for use in the password, so if you want to put vowels back in
    // or add special characters such as exclamation marks, this is where
    // you should do it
    $possible = "012346789bcdfghjkmnpqrtvwxyz-";

    // we refer to the length of $possible a few times, so let's grab it now
    $maxlength = strlen($possible);
  
    // check for length overflow and truncate if necessary
    if ($length > $maxlength) {
      $length = $maxlength;
    }
	
    // set up a counter for how many characters are in the password so far
    $i = 0; 
    
    // add random characters to $password until $length is reached
    while ($i < $length) { 

      // pick a random character from the possible ones
      $char = substr($possible, mt_rand(0, $maxlength-1), 1);
        
      // have we already used this character in $password?
      if (!strstr($password, $char)) { 
        // no, so it's OK to add it onto the end of whatever we've already got...
        $password .= $char;
        // ... and increase the counter by one
        $i++;
      }

    }

    // done!
    return $password;

  }

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Photo->id = $id;
		if (!$this->Photo->exists()) {
			throw new NotFoundException(__('Invalid photo'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Photo->save($this->request->data)) {
				$this->Session->setFlash(__('The photo has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The photo could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Photo->read(null, $id);
		}
		//$photos = $this->Photo->Photo->find('list');
		$users = $this->Photo->User->find('list');
		//$filters = $this->Photo->Filter->find('list');
		//$locations = $this->Photo->Location->find('list');
		//$urls = $this->Photo->Url->find('list');
		$this->set(compact('users'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Photo->id = $id;
		if (!$this->Photo->exists()) {
			throw new NotFoundException(__('Invalid photo'));
		}
		if ($this->Photo->delete()) {
			$this->Session->setFlash(__('Photo deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Photo was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
